import { computed, ref } from 'vue';
import { updateEntity, deleteEntity } from '@/api/lms/subjects';
import {
  updateKnowledge,
  createKnowledgeRelations,
  moveKnowledge,
  deleteKnowledge,
} from '@/api/problems/knowledges';
import { Modal, Message, Alert } from '@arco-design/web-vue';
import { validateForm } from '@/utils';
import { resolveTransitionHooks } from 'vue';

export let modalType = ref(0);
export let currRecord = ref({} as any);

// export function addNew() {
//   modalType.value = 3;
//   currRecord.value = {
//     enabled: true,
//   };
//   validateForm(null, true);
// }

// export function btnEdit(record: any) {
//   currRecord.value = { ...record };
//   modalType.value = 2;
//   validateForm(null, true);
// }

// export function btnDelete(record: any, call: Function) {
//   Modal.confirm({
//     title: '确认',
//     content: `确定要删除【${record.subjectName}】吗？`,
//     onOk: async function () {
//       await deleteEntity(record.subjectId);
//       call();
//     },
//   });
// }

// export async function btnEditSave(call: Function) {
//   if (!validateForm()) {
//     return;
//   }
//   modalType.value = 0;
//   await updateEntity(currRecord.value);
//   call();
// }

export async function btnSaveKnowledge() {
  if (!validateForm()) {
    return;
  }

  await updateKnowledge(currRecord.value);
}

export async function btnDeleteKnowledge(childECharts: any, knowledge: any) {
  if (!validateForm()) {
    return;
  }

  let command = {} as any;
  command.id = knowledge.pointId;
  let success = await deleteKnowledge(command);
  if (success) {
    Message.success('删除知识点成功');
    childECharts.value.deleteNodeEvent(knowledge);
  } else {
    Message.warning('删除知识点失败');
  }
}

export async function btnCreateKnowledge(childECharts: any, knowledge: any) {
  if (!validateForm()) {
    return;
  }

  let newKnowledge = await updateKnowledge(currRecord.value);
  if (newKnowledge) {
    let command = {} as any;
    command.sourcePointId = newKnowledge.pointId;
    command.targetPointId = currRecord.value.parentPointId;
    command.relationTypeId = 671;
    var relation = await createKnowledgeRelations(command);
    if (relation) {
      Message.success('创建新的知识点成功');
      childECharts.value.addNodeEvent(newKnowledge, knowledge.pointId);
    }
  } else {
    Message.warning('创建新的知识点失败');
  }
}

export async function btnMoveKnowledge(childECharts: any, knowledge: any) {
  if (!validateForm()) {
    return;
  }

  let command = {} as any;
  command.pointId = currRecord.value.pointId;
  command.parentPointId = 0;
  command.targetParentPointId = currRecord.value.movePointId;
  var success = await moveKnowledge(command);
  if (success) {
    Message.success('移动知识点成功');
    childECharts.value.moveNodeEvent(knowledge, currRecord.value.movePointId);
  } else {
    Message.warning('移动知识点失败');
  }
}
